package com.zq.zqmanager.service;

import com.alibaba.fastjson.JSONObject;
import com.zq.zqmanager.job.MyJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

/**
 * @program: zqmanager
 * @Date: 2020/3/9 8:21
 * @Author: zhengqiang
 * @Description:
 */
@Service
public class QuartzService {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    public void add(String params){
        //获取任务调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JSONObject json = JSONObject.parseObject(params);
        String jobName = json.getString("jobName");
        String jobGroupName = json.getString("jobGroupName");
        String triggerName = json.getString("triggerName");
        String triggerGroupName = json.getString("triggerGroupName");
        String cron = json.getString("cron");

        //创建
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity(jobName,jobGroupName).build();
        //放入参数
        jobDetail.getJobDataMap().put("name","郑强");

        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName,triggerGroupName)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();

        try {
            scheduler.scheduleJob(jobDetail,trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }


    public void delete(String params){
        //获取任务调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JSONObject json = JSONObject.parseObject(params);
        String jobName = json.getString("jobName");
        String jobGroupName = json.getString("jobGroupName");
        String triggerName = json.getString("triggerName");
        String triggerGroupName = json.getString("triggerGroupName");
        String cron = json.getString("cron");

        try {

            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);
            //停止触发器
            scheduler.pauseTrigger(triggerKey);
            //删除触发器
            scheduler.unscheduleJob(triggerKey);
            //删除任务
            JobKey jobKey = JobKey.jobKey(jobName,jobGroupName);
            scheduler.deleteJob(jobKey);

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void modify(String params){
        //获取任务调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JSONObject json = JSONObject.parseObject(params);
        String jobName = json.getString("jobName");
        String jobGroupName = json.getString("jobGroupName");
        String triggerName = json.getString("triggerName");
        String triggerGroupName = json.getString("triggerGroupName");
        String cron = json.getString("cron");

        //第一种方法先删除，再添加，添加之前先要获取到jobDetail，进而获取到class，
//        try {
//            JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(jobName,jobGroupName));
//            Class clazz = jobDetail.getJobClass();
//            //删除任务
//            delete(params);
//            json.put("cron","0/10 * * * * ?");
//            add(JSONObject.toJSONString(json));
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }

        //
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);
        try {
            CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            String oldCron = cronTrigger.getCronExpression();
            if(oldCron.equals(cron)) {
                System.out.println("定时表达式相同");
                return;
            }

            //组建新的trigger
            cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
